<template>
  <div class="video-box">
    <div v-if="!data" class="no-video video-desc-box">
      <i class="el-icon-video-camera"></i>
    </div>
    <template v-else>
      <div v-if="videoOffline" class="video-offline video-desc-box">
        <el-image :src="videoOfflineIcon"></el-image>
        <div class="text">视频离线</div>
      </div>
      <div class="video-main" v-if="videoOnline">
        <div class="video-desc-box" v-if="errorExceed">
          <div class="text-center">
            <div>该视频暂时无法查看,请查看切换视频</div>
            <br>
            <i @click="reTryVideo" class="el-icon-video-play"></i>
          </div>
        </div>
        <div class="video" ref="video"></div>
        <div class="video-info" v-if="showVideoTop">
          <div class="left" v-if="showVideoInfo">
            {{ data.machineName }}
          </div>
          <div class="right">
            <el-image
              class="status-img"
              v-if="statusIcon"
              :src="statusIcon"
              fit="contain"
            ></el-image>
            {{ statusText }}
            <i
              v-if="showCloseIcon"
              @click="handleCloseVideo"
              class="el-icon-close icon"
            ></i>
          </div>
        </div>
      </div>
    </template>
  </div>
</template>

<script>
// 如果视频没显示 请设置父级高度
// 如果视频没显示 请设置父级高度
// 如果视频没显示 请设置父级高度

// 视频-正常
import videoNormalIcon from "@/assets/icon_camera_online.png";
// 视频-离线
import videoOfflineIcon from "@/assets/icon_camera_offline.png";
// 视频-报警
import videoAlarmIcon from "@/assets/icon_camera_alarm.png";

// import 'xgplayer';
// import FlvPlayer from 'xgplayer-flv';
// import FlvJsPlayer from 'xgplayer-flv.js';
// import {getVideoUrl} from "@/api/video";
// import { videoBaseUrl } from '@/config';

// import Player from 'xgplayer'
// import HlsPlugin from 'xgplayer-hls'
// import hlsjsPlayer from 'xgplayer-hls.js';

const iconEnum = {
  1: {
    icon: videoNormalIcon,
    text: "在线",

  },
  2: {
    icon: videoOfflineIcon,
    text: "离线",
  },
};
const videoStatusEnum = {
  online: {
    status: "1",
    text: "在线",
  },
  offline: {
    status: "2",
    text: "离线",
  },
};

export default {
  name: "v-video",
  components: {},
  props: {
    showVideoInfo: {
      type: Boolean,
      default: false,
    },
    showCloseIcon: {
      type: Boolean,
      default: false,
    },
    data: {
      type: Object,
      default() {
        return {};
      },
    },
    playerConf: {
      type: Object,
      default() {
        return {};
      },
    },
    playUrl:{
      type:String,
      default:''
    }
  },
  data() {
    return {
      videoOfflineIcon,
      player: null,
      errorTimes: 0,
      videoOnline:true,//暂时默认在线
      videoOffline:false,
      statusText:'在线',
      statusIcon:'videoNormalIcon'
    };
  },
  computed: {
    showVideoTop() {
      return this.showVideoInfo || this.showCloseIcon
    },
    // videoOnline() {
    //   if (!this.data) {
    //     return false;
    //   }
    //   const { onlineStatus = videoStatusEnum.offline.status } = this.data;
    //   return String(onlineStatus) !== videoStatusEnum.offline.status;
    // },
    // videoOffline() {
    //   if (!this.data) {
    //     return false;
    //   }
    //   const { onlineStatus = videoStatusEnum.offline.status } = this.data;
    //   return String(onlineStatus) === videoStatusEnum.offline.status;
    // },
    // statusText() {
    //   if (!this.data) {
    //     return "";
    //   }
    //   const { onlineStatus = videoStatusEnum.offline.status } = this.data;
    //   return iconEnum[onlineStatus].text;
    // },
    // statusIcon() {
    //   if (!this.data) {
    //     return "";
    //   }
    //   const { onlineStatus = videoStatusEnum.offline.status } = this.data;
    //   return iconEnum[onlineStatus].icon;
    // },
    errorExceed() {
      return this.errorTimes >= 1;
    },
  },
  watch: {
    data: {
      handler(newVal, oldVal) {
        if (newVal === oldVal) {
          return newVal;
        }
        if (!newVal) {
          this.destroyVideo();
          return newVal;
        }
        this.errorTimes = 0
        this.$nextTick(() => {
          this.initVideo(newVal);
        });
      },
      immediate: true,
    },
    playUrl:{
      handler(n,o){
        if(n){
           if (!n) {
            this.destroyVideo();
            return n;
          }
          this.errorTimes = 0
          this.$nextTick(() => {
            console.log('this.playUrl===',this.playUrl)
            this.initVideo(n);
          });
        }
      },
      deep:true,
      immediate:true
    }
  },
  mounted() {
    // console.log('如果视频没显示 请设置父级高度');
  },
  methods: {
    handleCloseVideo() {
      this.$emit("close-video", this.data);
      this.destroyVideo()
    },
    async reTryVideo() {
      await this.destroyVideo()
      this.errorTimes = 0
      this.initVideo()
    },
    initVideo(){
      if(this.playUrl){
        const url = this.playUrl
        const isFlv = url.indexOf('.flv') > -1
        const isM3u8 = url.indexOf('.m3u8') > -1
        if(isM3u8){
          this.player = new Player({
              el: this.$refs.video,
              url: url, // hls 流地址
              autoplay: true,
              autoplayMuted: true,
              cors: true,
              screenShot: true,
              isLive: true,
              fluid: true,
              ignores: ['volume'],
              plugins: [window.HlsPlayer] // 第二步
          })
        }else if(isFlv){
          console.log('this.$refs.video===',this)
          this.player = new Jessibuca({
            container: this.$refs.video,
            videoBuffer: 0, // 缓存时长
            isResize: false,
            text: "",
            loadingText: "加载中",
            hiddenAutoPause: false,
            debug: false,
            isFullResize: true,
            hasAudio: false,
            showBandwidth: false, // 显示网速
            heartTimeoutReplay: true,
            operateBtns: {
              fullscreen: true,
              play: true,
              audio: false,
            },
            forceNoOffscreen: true,
            supportDblclickFullscreen: true,
            isNotMute: false,
          }).on('error', (e) => {
            console.log('error', e)
            this.errorTimes++
          });
          this.player.play(url)
        }
      }else{
        this.destroyVideo();
      }
      
    },
    // async initVideo(data = this.data) {
    //   await this.destroyVideo()

    //   if (String(data.onlineStatus) !== videoStatusEnum.online.status) {
    //     return false;
    //   }

    //   const urlRes = await getVideoUrl({
    //     machineNum: data.machineNum,
    //     id: data.id,
    //   })

    //   console.log('urlRes', urlRes)
    //   if(urlRes.code === 200) {
    //     const url = urlRes.data.playUrl
    //     const isFlv = url.indexOf('.flv') > -1
    //     const isM3u8 = url.indexOf('.m3u8') > -1
    //     if(isM3u8){
    //       this.player = new Player({
    //           el: this.$refs.video,
    //           url: url, // hls 流地址
    //           autoplay: true,
    //           autoplayMuted: true,
    //           cors: true,
    //           screenShot: true,
    //           isLive: true,
    //           fluid: true,
    //           ignores: ['volume'],
    //           plugins: [window.HlsPlayer] // 第二步
    //       })
    //     }else if(isFlv){
    //       this.player = new Jessibuca({
    //         container: this.$refs.video,
    //         videoBuffer: 0, // 缓存时长
    //         isResize: false,
    //         text: "",
    //         loadingText: "加载中",
    //         hiddenAutoPause: false,
    //         debug: false,
    //         isFullResize: true,
    //         hasAudio: false,
    //         showBandwidth: false, // 显示网速
    //         heartTimeoutReplay: true,
    //         operateBtns: {
    //           fullscreen: true,
    //           play: true,
    //           audio: false,
    //         },
    //         forceNoOffscreen: true,
    //         supportDblclickFullscreen: true,
    //         isNotMute: false,
    //       }).on('error', (e) => {
    //         console.log('error', e)
    //         this.errorTimes++
    //       });
    //       this.player.play(url)
    //     }
    //   }else{
    //     this.destroyVideo();
    //   }


    //   //
    //   // this.player.on("error", (e)=> {
    //   //   console.error("发生了错误", e);
    //   //   if (this.errorExceed) {
    //   //   } else {
    //   //     // this.initVideo();
    //   //   }
    //   // });
    // },
    destroyVideo() {
      return new Promise((resolve) => {
        this.player?.destroy();
        setTimeout(() => {
          this.player = null;
          resolve();
        });
      });
    },
  },
  async beforeDestroy() {
    await this.destroyVideo();
  },
};
</script>

<style lang="scss" scoped>
.video-box {
  width: 100%;
  height: 100%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  &:hover {
    .video-main {
      .video-info {
        .right {
          .icon {
            opacity: 1;
          }
        }
      }
    }
  }

  .video-desc-box {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #000000;
    font-size: 16px;
    z-index: 999;
    color: #fff;
    i {
      font-size: 58px;
      color: #fff;
    }
    .text {
      margin-left: 10px;
    }
  }
  // .video-offline{

  // }
  // .no-video {
  // }
  .video-main {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    .video {
      max-width: 100%;
      max-height: 100%;
      background-color: #000;
      height: 100% !important;
      padding-top: 0 !important;
    }
    .video-info {
      position: absolute;
      left: 0;
      top: 0;
      right: 0;
      height: 38px;
      line-height: 38px;
      padding: 0 19px;
      font-size: 14px;
      color: #fff;
      cursor: default;
      display: flex;
      align-items: center;
      justify-content: space-between;
      background-color: rgba(0, 0, 0, 0.6);
      z-index: 999;
      // .left{
      // }
      .right {
        height: 38px;
        line-height: 38px;
        display: flex;
        align-items: center;
        .status-img {
          width: 13px;
          height: 15px;
          margin-right: 7px;
        }
        .icon {
          transition: all 0.3s;
          opacity: 0;
          cursor: pointer;
          font-size: 24px;
          padding: 5px;
        }
      }
    }
    ::v-deep {
      .xgplayer-screenshot {
        .name {
          span {
            display: none;
          }
          &:after {
            content: "抓拍";
            width: fit-content;
            padding: 0 10px;
            height: 20px;
            line-height: 20px;
            background: rgba(0, 0, 0, 0.38);
            border-radius: 10px;
          }
        }
      }
    }
  }
  .text-center{
    text-align: center;
  }
}
</style>
